home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / util / HexUtils.js < prev    next >
Encoding:
Text File  |  2007-04-11  |  2.3 KB  |  102 lines

  1. /****i* SOURCE_FILE/INFO
  2.   *
  3.   * NAME
  4.   *  HexUtils.js
  5.   *
  6.   * USAGE
  7.   *  Part of WSP JavaScript Library.
  8.   *
  9.   * COPYRIGHT
  10.   *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.   *  All Rights Reserved.
  12.   *
  13.   *  This is an unpublished work protected by Website Pros, Inc.
  14.   *  as a trade secret, and is not to be used or disclosed except as
  15.   *  expressly provided in a written license agreement executed by
  16.   *  you and Website Pros, Inc.
  17.   *
  18.   *      <copyright@websitepros.com>
  19.   *
  20.   * NOTES
  21.   *  JavaScript code.
  22.   *
  23.   *****/
  24.  
  25. if (!IS.isModuleInitialized("IS.NOF.UTIL.HexUtils"))
  26. {
  27.     /****h* NOF_JavaScript_Library/NOF.UTIL.HexUtils
  28.         *
  29.         * NAME
  30.         *  NOF.UTIL.HexUtils
  31.         *
  32.         * DESCRIPTION
  33.         *    
  34.         * The <code>HexUtils</code> class contains code to convert hex into other format and viceversa
  35.         *
  36.         ****/
  37.     function UTIL_HexUtils() {
  38.         this.__proto__ = UTIL_HexUtils.prototype;
  39.     }
  40.     {  
  41.         var method = UTIL_HexUtils.prototype;
  42.         
  43.         /**
  44.             * Convert number from hexa to decimal format
  45.             * @param hexa - the number to be converted
  46.             ***/
  47.         method.hexToDec = function (/*string*/ hexa) {     
  48.             var temp = parseInt(hexa, 16);
  49.             return temp;
  50.         }
  51.         
  52.         /**
  53.             * Convert number from decimal to hexa format
  54.             * @param decimal - the number to be converted
  55.             ***/
  56.         method.decToHex = function (/*string*/ decimal) {
  57.             var temp = parseInt(decimal,10);
  58.             
  59.             if (isNaN(temp)) { 
  60.                 return null;
  61.             }
  62.             
  63.             var hex = "";
  64.             while (temp > 15) {
  65.                 var remainder = temp % 16;
  66.                 hex = this.getHexDigit(remainder) + "" + hex;
  67.                 temp = parseInt(temp / 16);
  68.             }
  69.             if (temp >= 0) {
  70.                 hex =  this.getHexDigit(temp) + "" + hex;
  71.             }
  72.             else {    
  73.                 return 0;
  74.             }
  75.             
  76.             var size  = hex.length;
  77.             var zeros = "";
  78.             if ( size != 6 ) {
  79.                 for (var j=0; j < 6 - size; j++ ) {
  80.                     zeros = zeros + "0";
  81.                 }
  82.             }
  83.             return zeros + hex;
  84.         }
  85.         
  86.         // private method    
  87.         method.getHexDigit = function (/*string*/ decimal) {
  88.             var hexArray = Array("A", "B", "C", "D", "E", "F");
  89.             var hexVal = parseInt(0 + decimal, 10);
  90.             
  91.             if ((hexVal) > 15) {
  92.                 return null;
  93.             }
  94.             else {
  95.                 hexVal = hexVal < 10 ? hexVal : hexArray[hexVal-10];
  96.             }
  97.             return hexVal;
  98.         }    
  99.     }
  100.     
  101.     UTIL.__proto__.HexUtils = new UTIL_HexUtils();    
  102. }